﻿package xFGE.xDisplay.xUI
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class xScrollBar2 extends MovieClip
	{
		private var _bar:xButton;
		private var _up:xButton;
		private var _down:xButton;
		private var _backGround:xButton;
		//bar最小y位标和最大y坐标
		private var myMaxVerticalScroll:Number = 451;
		private var myMinVerticalScroll:Number = 40;
		//内容长度与bar长度比例
		private var scrollScale:Number;
		
		private var _present:Number;//当前要显示的百份比 0-100
		
		private var _board:MovieClip;
		private var _windowHeight:int;
		
		public function xScrollBar2()
		{
			var e:Error
			
			_board = new MovieClip();
			_present = 0;
			_bar = this.getChildByName("__xbtn_bar") as xButton;
			if(_bar == null)
			{
				e = new Error("缺 __xbtn_bar");
				throw e;
			}
			_up = this.getChildByName("__xbtn_up") as xButton;
			if(_up == null)
			{
				e = new Error("缺 __xbtn_up");
				throw e;
			}
			_down = this.getChildByName("__xbtn_down") as xButton;
			if(_down == null)
			{
				e = new Error("缺 __xbtn_down");
				throw e;
			}
			
			myMinVerticalScroll =  _up.y + _up.height;
			myMaxVerticalScroll = _down.y - _bar.height;
			
			_bar.y = myMinVerticalScroll;
			
			_bar.addEventListener(MouseEvent.MOUSE_DOWN,strartDragHandler);		
			_up.addEventListener(MouseEvent.MOUSE_DOWN,upHandler);		
			_down.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			this.addEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
		}
		private function strartDragHandler(evt:Event):void
		{
			var rec:Rectangle = new Rectangle(0,myMinVerticalScroll,0,myMaxVerticalScroll-_down.height);
			this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			this.stage.addEventListener(MouseEvent.MOUSE_UP,stopDragHandler);
			_bar.startDrag(false,rec);
		}
		private function stopDragHandler(eve:Event):void
		{
			this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragHandler);
			_bar.stopDrag();
		}
		private function enterFrameHandler(evt:Event):void
		{
//			var e:MyEvent = new MyEvent("scroll_bar");
			
			_present = (_bar.y - myMinVerticalScroll)/(myMaxVerticalScroll - myMinVerticalScroll);
			if(_present < 0)
			{
				_present = 0;
			}
			if(_present > 1)
			{
				_present = 1 
			}
			
			var upL:int = _board.height - _windowHeight;//_panelMC.__mc_window.height;
			_board.y = -upL*_present;
			
//			e.myObject = new Object();
//			e.myObject.present = _present;
//			this.dispatchEvent(e);
		}
		private function downHandler(evt:Event):void
		{
			goDown();
		}
		private function upHandler(evt:Event):void
		{
			goUp();
		}
		private function goUp():void
		{
			var testPresent:Number = ((_bar.y-1) - myMinVerticalScroll)/(myMaxVerticalScroll - myMinVerticalScroll);
			if(testPresent > 0)
			{
				_bar.y--;
				_present = (_bar.y - myMinVerticalScroll)/(myMaxVerticalScroll - myMinVerticalScroll);	
				var upL:int = _board.height - _windowHeight;//_panelMC.__mc_window.height;
				_board.y = -upL*_present;
			}
			else
			{
				var upL:int = _board.height - _windowHeight;//_panelMC.__mc_window.height;
				_board.y = -upL*0;
			}
		}
		private function goDown():void
		{
			var testPresent:Number = ((_bar.y+1) - myMinVerticalScroll)/(myMaxVerticalScroll - myMinVerticalScroll);
			if(testPresent < 1)
			{
				_bar.y++;
				_present = (_bar.y - myMinVerticalScroll)/(myMaxVerticalScroll - myMinVerticalScroll);	
				var upL:int = _board.height - _windowHeight;//_panelMC.__mc_window.height;
				_board.y = -upL*_present;
			}
			else
			{
				var upL:int = _board.height - _windowHeight;//_panelMC.__mc_window.height;
				_board.y = -upL;
			}
		}
		private function wheelHandler(evt:MouseEvent):void
		{
			trace(evt.delta);
			if(evt.delta > 0)
			{
				goUp();
			}
			else
			{
				goDown();
			}
		}
		public function setBoard(b:MovieClip,w:int):void
		{
			_board = b;
			_windowHeight = w;
			
		}
		
	}
}